home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7440 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.8 KB  |  171 lines

  1. Path: news.u-net.com!news
  2. From: charlotte@parmo.u-net.com (Thomas Christensen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Call order of constructors
  5. Date: Fri, 23 Feb 1996 09:55:43 GMT
  6. Organization: U-NET limited
  7. Message-ID: <4gk30t$pqt@nuntius.u-net.net>
  8. References: <4ftv2n$kts@ccnet3.ccnet.com>
  9. Reply-To: thc@mailhost.net
  10. NNTP-Posting-Host: parmo.u-net.com
  11. X-Newsreader: Forte Free Agent v0.55
  12.  
  13. jantypas@ccnet.com (John Antypas) wrote:
  14.  
  15. >Hello C++ wizards...
  16.  
  17. >I'm trying to create a library access some private netowrk resources. 
  18. >I thought I'd try something like this:
  19.  
  20. >class TRANSPORT {
  21. >    ... magic stuff...
  22. >public:
  23. >    TRANSPORT(char *name)    // Called to activate and bind transport(name)
  24. >    ~TRANSPORT()        // Called when we're done with transport(name)
  25.  
  26. >}
  27.  
  28. >class SESSION(
  29. >    TRANSPORT transport&;
  30. >    ....
  31. >    SESSION(char *transport_name, 
  32. >        char *transport_address);
  33. >    ~SESSION();
  34. >    ....
  35. >}
  36.  
  37. >I was hoping that main code such as :
  38.  
  39. >    SESSION    my_session("iee488", "unit-4B");
  40.  
  41. >would create a session on the IEEE488 controller, to unit Unit-4B, specifically,    - open a trasnport to the IEEE-488 system by calling the IEEE488
  42. >      constructor
  43. >    - open a session by calling the session constructor and have it
  44. >      finish the job.
  45.  
  46. >The destructor sequence runs in the exact reverse.
  47.  
  48. >First off, the compiler complains about the TRANSPORT constructors, second,
  49. >I find myself asking what to do if a constructor or destructor fails?
  50. >How do I catch the error condition to notify outer levels of the program?
  51.  
  52. >Help as always is appreciated.
  53.  
  54. >John Antypas    jantypas@soft21.s21.com
  55.  
  56. Hi John
  57.  
  58. 1. The constructor situation
  59. I would change the SESSION class to something like this:
  60.  
  61. class SESSION(
  62.     TRANSPORT &transport;
  63.     ....
  64.     SESSION(char *transport_name, char *transport_address)
  65.     {
  66.         transport = NULL;
  67.         transport = new TRANSPORT(transport_address);
  68.     }
  69.     ~SESSION()
  70.     {
  71.         if (transport)
  72.             delete transport;
  73.     }
  74.     ....
  75. }
  76.  
  77. In this way you control the construction of the TRANSPORT object.
  78.  
  79. 2. The error handling situation
  80. You said the word: catch!
  81. I would use exception handling to catch the errors so if the TRANSPORT
  82. couldn't connect then I throw an exception, which you'd have to catch
  83. in the main program like this:
  84. main()
  85. {
  86.     try
  87.     {
  88.         SESSION    my_session("iee488", "unit-4B");
  89.     }
  90.     catch(char *text)
  91.     {
  92.         cout << "Exception thrown: " << text << endl;
  93.         // Do some more magic...
  94.     }
  95.     ...
  96. }
  97.  
  98. The exception need to be thrown from anyone of the TRANSPORT or
  99. SESSION classes, and the syntax is like that of a return statement:
  100.     throw "Transport object couldn't connect!";
  101. You can even throw a c++ class containing further description of the
  102. exception ect.
  103. The real power of C++ exception handling lies not only in its ability
  104. to deal with exceptions of varying types, but also in its ability to
  105. automatically call destructor functions during stack unwinding for all
  106. local objects constructed before the exception was thrown.
  107.  
  108. 3. The alternative solution situation
  109. As always there's  numerous ways to do this. Here's how Microsoft
  110. would do it:
  111.  
  112. Initializing Member Objects
  113. Classes can contain member objects of class type, but to ensure that
  114. initialization requirements for the member objects are met, one of the
  115. following conditions must be met:
  116. ╖    The contained objectÆs class requires no constructor.
  117. ╖    The contained objectÆs class has an accessible default
  118. constructor.
  119. ╖    The containing classÆs constructors all explicitly initialize the
  120. contained object.
  121.   
  122. The following example shows how to perform such an initialization:
  123.   
  124. // Declare a class Point.
  125. class Point
  126. {
  127. public:
  128.     Point( int x, int y ) { _x = x; _y = y; }
  129. private:
  130.     int _x, _y;
  131. };
  132.  
  133. // Declare a rectangle class that contains objects of type Point.
  134. class Rect
  135. {
  136. public:
  137.     Rect( int x1, int y1, int x2, int y2 );
  138. private:
  139.     Point _topleft, _bottomright;
  140. };
  141.  
  142. //  Define the constructor for class Rect. This constructor
  143. //   explicitly initializes the objects of type Point.
  144. Rect::Rect( int x1, int y1, int x2, int y2 ) :
  145. _topleft( x1, y1 ), _bottomright( x2, y2 )
  146. {
  147. }
  148.   
  149. The Rect class, shown in the preceding example, contains two member
  150. objects of class Point. Its constructor explicitly initializes the
  151. objects _topleft and _bottomright. Note that a colon follows the
  152. closing parenthesis of the constructor (in the definition). The colon
  153. is followed by the member names and arguments with which to initialize
  154. the objects of type Point.
  155.  
  156. Warning   The order in which the member initializers are specified in
  157. the constructor does not affect the order in which the members are
  158. constructed; the members are constructed in the order in which they
  159. are declared in the class.
  160.   
  161. Reference and const member objects must be initialized using the
  162. member initialization syntax shown in Syntax in Initializing Bases and
  163. Members. There is no other way to initialize these objects.
  164.  
  165. Hope you can use some of this...
  166.  
  167. Thomas Christensen
  168. E-me-at: thc@mailhost.net
  169.  
  170.  
  171.